<?php

namespace App\Http\Controllers\Admin;

use Validator;
use App\ArticleType;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class ArticleTypeController extends Controller
{
    public function index(Request $request)
    {
        $articleTypes = ArticleType::get();
        return view('admin.article_type.index', [
            'request' => $request,
            'articleTypes' => $articleTypes
        ]);
    }

    public function add(Request $request)
    {
        if ($request->isMethod('post')) {
            $data = $request->except(['_token']);
            $rules = [
                'name' => 'required',
                'type' => 'in:0,1'
            ];
            $messages = [
                'name.required' => '请输入栏目名！',
                'type.in' => '请选择栏目类型！'
            ];
            if ($data['type'] == '1') {
                $rules['content'] = 'required';
                $messages['content.required'] = '请输入栏目内容！';
            }
            $validator = Validator::make($data, $rules, $messages);
            if ($validator->fails()) {
                $return = [
                    'code' => __LINE__,
                    'msg' => $validator->errors()->first()
                ];
                return response()->json($return);
            }
            $articleType = new ArticleType();
            $articleType->name = $data['name'];
            $articleType->type = $data['type'];
            if (!empty($data['route'])) {
                $articleType->route = $data['route'];
            }
            if ($data['type'] == '1') {
                $articleType->content = $data['content'];
                $articleType->thumbnail = $data['thumbnail'];
            }
            $articleType->save();
            $return = ['code' => 0];
            return response()->json($return);
        }
        return view('admin.article_type.add', [
            'request' => $request
        ]);
    }

    public function edit(Request $request, $id)
    {
        if ($request->isMethod('post')) {
            $data = $request->except(['_token']);
            $rules = [
                'name' => 'required',
                'type' => 'in:0,1'
            ];
            $messages = [
                'name.required' => '请输入栏目名！',
                'type.in' => '请选择栏目类型！'
            ];
            if ($data['type'] == '1') {
                $rules['content'] = 'required';
                $messages['content.required'] = '请输入栏目内容！';
            }
            $validator = Validator::make($data, $rules, $messages);
            if ($validator->fails()) {
                $return = [
                    'code' => __LINE__,
                    'msg' => $validator->errors()->first()
                ];
                return response()->json($return);
            }
            $articleType = ArticleType::find($data['id']);
            $articleType->name = $data['name'];
            if (!empty($data['route'])) {
                $articleType->route = $data['route'];
            }
            if ($data['type'] == '1') {
                $articleType->content = $data['content'];
                if (!empty($data['thumbnail'])) {
                    $articleType->thumbnail = $data['thumbnail'];
                }
            }
            $articleType->save();
            $return = ['code' => 0];
            return response()->json($return);
        }
        $articleType = ArticleType::find($id);
        return view('admin.article_type.edit', [
            'request' => $request,
            'articleType' => $articleType
        ]);
    }

    public function del(Request $request)
    {
        $id = $request->input('id');
        $articleType = ArticleType::find($id);
        if ($articleType->type == '1') {
            $file = 'upload/' . iconv('utf-8', 'gbk', $articleType->thumbnail);
            if (is_file($file)) {
                unlink($file);
            }
        }
        if ($articleType->delete()) {
            $return = ['code' => 0];
        } else {
            $return = [
                'code' => __LINE__,
                'msg' => '删除失败，请稍候重试！'
            ];
        }
        return response()->json($return);
    }
}